package edu.sias.hyznxfapi.checkCompliance.checkAudioFre.utils.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.sias.hyznxfapi.checkCompliance.checkAudioFre.utils.MyUtil;

import java.util.Map;

public class CheckAudioFre {
    private static final String url = "https://audit.iflyaisol.com/audit/v2/audio";
    private static final String queryUrl = "https://audit.iflyaisol.com/audit/v2/query";

    public static final String APPID = "7667b019";
    public static final String APISecret = "YWQ3ZDJkMGFkMTA4NWU1ZjFiMTk3Njdm";
    public static final String APIKey = "9f7d839e56bbe3cff02dc462e8e7f8c3";


    /**
     * 只需要传输音频地址即可
     * @param audioUrl
     * @return
     * @throws Exception
     */
    public String AudioFreCheck(String audioUrl) throws Exception {
        String suggest = "";
        // 1、业务参数，注意notify_url替换自己的回调地址
        String json = "{\n" +
                "  \"audio_list\": [\n" +
                "    {\n" +
                "      \"audio_type\": \"" + audioUrl.substring(audioUrl.length() - 3) + "\",\n" +
                "      \"file_url\": \"" + audioUrl + "\",\n" +
                "      \"name\": \"" + "xxx" + audioUrl.substring(audioUrl.length() - 8) + "\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"notify_url\": \"http://wdfgdzx.top:8000/user/audio_video_callback\"\n" +
                "}";
        // 2、获取鉴权
        Map<String, String> urlParams = MyUtil.getAuth(APPID, APIKey, APISecret);
        ///3、发起请求
        String returnResult = MyUtil.doPostJson(url, urlParams, json);
        // System.out.println("请到回调地址或存储库查看结果，调用信息如下：\n" + returnResult);
        String request_id = (String) JSONObject.parseObject(JSONObject.parseObject(returnResult).get("data").toString()).get("request_id");
        //4、拿到request_id后主动查询合规结果
        while (true) {
            String queryJson = "{\n" +
                    "  \"request_id\": \"" + request_id + "\"\n" +
                    "}";
            urlParams = MyUtil.getAuth(APPID, APIKey, APISecret);
            returnResult = MyUtil.doPostJson(queryUrl, urlParams, queryJson);
            Integer finishIndex = (Integer) JSONObject.parseObject(JSONObject.parseObject(returnResult).get("data").toString()).get("audit_status");
            if (finishIndex == 0) {
                System.out.println("音频合规待审核...");
            }
            if (finishIndex == 1) {
                System.out.println("音频合规审核中...");
            }
            if (finishIndex == 2) {
                System.out.println("音频合规审核完成：");
                System.out.println(returnResult);
                break;
            }
            if (finishIndex == 4) {
                System.out.println("音频合规审核异常：");
                System.out.println(returnResult);
            }
            Thread.sleep(3000);
        }
        JSONObject jsonObject = JSON.parseObject(returnResult);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray result_list = data.getJSONArray("result_list");
        for (int i = 0; i < result_list.size(); i++){
            String suggest1 = result_list.getJSONObject(i).getString("suggest");
            suggest = suggest1;
            System.out.println("suggest" + suggest);
        }
        if ((Integer) JSONObject.parseObject(JSONObject.parseObject(returnResult).get("data").toString()).get("audit_status") == 2){
            System.out.println("通过");
            return suggest;
        }else {
            return "音频合规审核异常,请及时联系工作人员！";
        }
    }

    public static void main(String[] args) {
        try {
            String s = new CheckAudioFre().AudioFreCheck("https://edu-subject-1.oss-cn-beijing.aliyuncs.com/test/3.mp3");
            System.out.println("验证" + s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
